package Util;
import java.awt.BorderLayout;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import redis.clients.jedis.Jedis;
public class Myframe extends JFrame  {
	public 
	JLabel myLabel1;
	JLabel myLabel2;
	JLabel myLabel3;
	JPanel LoginPanel;
	JPanel panel1;
	JPanel panel2;
	JPanel panel3;
	JPanel panel4;
	JRadioButton Button1;
	JRadioButton Button2;
	JButton Button3;
	JButton Button4;
	JTextField  myTextField1;
	JTextField myTextField2;
	JTextField  myTextField3;
	JTextField myTextField4;
	Jedis jedis= new Jedis("127.0.0.1",6379);
	 
	public Myframe(){
		LayoutManager layoutManager = new GridLayout(4,1);
        setLayout(layoutManager);
		setTitle("书店销售系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(400, 400, 485, 343);
		setLocationRelativeTo(null);
		LoginPanel = new JPanel(new GridLayout(4,1));
		LoginPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		// setContentPane(LoginPanel);
		LoginPanel.setLayout(null);
		ImageIcon backimg;
		backimg=new ImageIcon("C:\\Users\\86132\\Pictures\\Cyberpunk 2077\\1.jpg");
		
		myLabel1 = new JLabel("用户名");
		myLabel1.setFont(new Font("新宋体", Font.BOLD, 20));
		myLabel1.setForeground(Color.LIGHT_GRAY);
		myLabel1.setBounds(49, 170, 91, 38);
		myLabel1.setOpaque(false);
		LoginPanel.add(myLabel1);
		 
		myLabel2 = new JLabel("密码");
	
		myLabel2.setFont(new Font("新宋体", Font.BOLD, 20));
		myLabel2.setForeground(Color.LIGHT_GRAY);
		myLabel2.setBounds(49, 220, 91, 38);
		myLabel2.setOpaque(false);
		 
		myLabel3 = new JLabel("权限");
	
		myLabel3.setFont(new Font("新宋体", Font.BOLD, 20));
		myLabel3.setForeground(Color.LIGHT_GRAY);
		myLabel3.setBounds(49, 270, 91, 38);
		myLabel3.setOpaque(false);
		myTextField1 = new JTextField(8);//八位账户
		myTextField2 = new JTextField(8);//八位密码
		myTextField3 = new JTextField(8);//权限书写管理员为1，销售为2
		
    	/*Button1 = new JRadioButton("权限管理，1为管理员，2 为销售员");
    	Button1.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			 try (Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String root=null; 
	                if(myTextField3.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"权限不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
			}
	                else if(!myTextField1.getText().equals("")){

	                      root = myTextField1.getText();
	                   
	                    int flag =0;
	                    ResultSet re = st.executeQuery("select  root from userr");
	                    while(re.next()){
	                        String root1 = re.getString(1);
	                        if(root1.equals(root )){
	                            flag=1;
	                        }
	                    }

	                    if(flag==0){
	                        String sql2= "insert into userr values('"+username+"','"+userpassed+"')";
	                        st.executeUpdate(sql2);
	                        JOptionPane.showMessageDialog(null,"注册成功","成功",
	                                JOptionPane.INFORMATION_MESSAGE);
	                    }else if(flag==1){
	                        JOptionPane.showMessageDialog(null,"用户名已存在","错误",
	                                JOptionPane.WARNING_MESSAGE);

	                    }
	                
	                }
	                }
			 catch (SQLException ex) {
	                ex.printStackTrace();
	            }
			 }
    		
    	});*/
    	
    	Button3 = new JButton("注册");
    	Button3.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try (Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                String username=null; 
	                String userpassed=null;
	                String root =null;
	                
	                
	               if(myTextField3.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"权限不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	               else if(myTextField1.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"用户名不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                else if( !myTextField1.getText().equals("")&&myTextField2.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"密码不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                else if(!myTextField1.getText().equals("")&&!myTextField2.getText().equals("")){
	                      username = myTextField1.getText();
	                   userpassed = myTextField2.getText();
	                   root = myTextField3.getText();
	                   jedis.set(username, userpassed);
	                    int flag =0;
	                    ResultSet re = st.executeQuery("select username from userr");
	                    while(re.next()){
	                        String username1 = re.getString(1);
	                        if(username1.equals(jedis.get(username))){
	                            flag=1;
	                        }
	                    }

	                    if(flag==0){
	                        String sql2= "insert into userr values('"+username+"','"+userpassed+"','"+root+"')";
	                        st.executeUpdate(sql2);
	                        JOptionPane.showMessageDialog(null,"注册成功","成功",
	                                JOptionPane.INFORMATION_MESSAGE);
	                    }else if(flag==1){
	                        JOptionPane.showMessageDialog(null,"用户名已存在","错误",
	                                JOptionPane.WARNING_MESSAGE);

	                    }
	                }
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
			}
    		
    	});
    	
    	Button4 = new JButton("登录");
    	Button4.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				try (Connection conn = Myutil.getConnection()){

	                java.sql.Statement st = conn.createStatement();
	                if(myTextField1.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"用户名不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                else if( !myTextField1.getText().equals("")&&myTextField2.getText().equals("")){
	                    JOptionPane.showMessageDialog(null,"密码不能为空","提示",
	                            JOptionPane.WARNING_MESSAGE);
	                }
	                else if(!myTextField1.getText().equals("")&&!myTextField2.getText().equals("")){
	                    String username = myTextField1.getText();
	                    String password = myTextField2.getText();
	                    int flag =0;
	                    ResultSet re = st.executeQuery("select username from userr");
	                    while(re.next()){
	                        String username1 = re.getString(1);
	                        if(username1.equals(username)){
	                            flag=1;
	                        }
	                    }

	                    if(flag==0){
	                        String sql2= "insert into userr values('"+username+"','"+password+"')";
	                        st.executeUpdate(sql2);
	                        JOptionPane.showMessageDialog(null,"登录成功","成功",
	                                JOptionPane.INFORMATION_MESSAGE);
	                    }else if(flag==1){
	                        JOptionPane.showMessageDialog(null,"登录成功","成功",
	                        		 JOptionPane.INFORMATION_MESSAGE);
	                        	 new jFrame1(); //打开第二个窗口后将第一个设置为不可见
	                        	setVisible(false);

	                    }
	                }
	            } catch (SQLException ex) {
	                ex.printStackTrace();
	            }
				// TODO Auto-generated method stub
			}
			});
    	panel1 = new JPanel();
    	panel2 = new JPanel();
    	panel3 = new JPanel();
    	panel4 = new JPanel();
    
    	panel2.add(myLabel1);
    	panel2.add(myTextField1);
    	panel3.add(myLabel2);
    	panel3.add(myTextField2);
    	panel3.add(myLabel3);
    	panel3.add(myTextField3);
    	panel4.add(Button3);
    	panel4.add(Button4);
    	
    	add(panel1);
    	add(panel2);
    	add(panel3);
    	add(panel4);
    	setVisible(true);
    	}
	}
	
	



	
	
	
	
	
	
	
	
	


